<template>
  <el-dialog v-model="dialogVisible" title="修改密码" width="500px" draggable>
    <el-form :model="ruleFormtwo" label-width="auto" ref="ruleFormRef">
      <el-form-item label="旧密码" prop="userName">
        <el-input v-model="ruleFormtwo.oldPassword" />
      </el-form-item>

      <el-form-item label="新密码" prop="password">
        <el-input v-model="ruleFormtwo.newPassword" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="savemmbutton(ruleFormRef)">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { resetPwdaApi } from "@/api/system/system.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useUserStore } from "@/stores/modules/user";
import { useRouter } from "vue-router";
let ruleFormtwo = ref({
  oldPassword: "", //用户ID
  newPassword: "" //密码
});
const userStore = useUserStore();
const router = useRouter();
const savemmbutton = () => {
  resetPwdaApi(ruleFormtwo.value).then(res => {
    if (res.code) {
      ElMessage({
        message: "修改成功",
        type: "success"
      });
      dialogVisible.value = false;
      ruleFormtwo.value = {
        oldPassword: "", //用户ID
        newPassword: "" //密码
      };
      // 2.清除 Token
      userStore.setToken("");
      sessionStorage.clear();
      // 3.重定向到登陆页
      router.replace("/login");
      ElMessage.success("退出登录成功！");
      location.reload();
    }
  });
};
const dialogVisible = ref(false);
const openDialog = () => {
  dialogVisible.value = true;
};

defineExpose({ openDialog });
</script>
